import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/home',
  },
  {
    path: '/login-register',
    name: 'LoginRegister',
    component: () => import('../views/login-register/LoginRegister'),
    children: [{
        path: '/login-register/register',
        name: 'Register',
        component: () => import('../views/login-register/children/Register'),
        meta: {
          hide: Symbol()
        }
      },
      {
        path: '/login-register/login',
        name: 'Login',
        component: () => import('../views/login-register/children/Login'),
        meta: {
          hide: Symbol()
        }
      },
      {
        path: '/login-register/bind',
        name: 'Bind',
        component: () => import('../views/login-register/children/Bind'),
        meta: {
          hide: Symbol()
        }
      },
      {
        path: '/login-register/forget',
        name: 'Forget',
        component: () => import('../views/login-register/children/Forget'),
        meta: {
          hide: Symbol()
        }
      }
    ]
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/home/Home'),
    meta: {
      title: '首页'
    }
  },
  // 新增PackMate认证页面
  {
    path: '/home/packmate',
    name: 'Packmate',
    component: () => import('../views/packmate-ident/Packmate'),
    meta: {
      title: 'Packmate认证'
    }
  },
  // 新增商家入驻页面
  {
    path: '/home/businessSettlement',
    name: 'BusinessSettlement',
    component: () => import('../views/business-settlement/BusinessSettlement'),
    meta: {
      title: '商家入驻'
    }
  },
  {
    path: '/home/allproduct',
    name: 'AllProduct',
    component: () => import('../views/home/AllProduct'),
  },
  {
    path: '/suggest',
    name: 'Suggest',
    component: () => import('../views/suggest/Suggest')
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import('../views/cate/Category')
  },
  {
    path: '/category/classify/:id',
    name: 'CateClassify',
    component: () => import('../views/cate/CateClassify'),
    meta: {
      keepAlive: false // 不需要被缓存
    }
  },
  {
    path: '/category/product/:id',
    name: 'CateProduct',
    component: () => import('../views/cate/CateProductDetails'),
    meta: {
      keepAlive: false // 不需要被缓存
    }
  },
  {
    path: '/category/order',
    name: 'CateProductOrder',
    component: () => import('../views/cate/Order'),
    meta: {
      keepAlive: false // 不需要被缓存
    }
  },
  {
    path: '/category/payment',
    name: 'OrderPayment',
    component: () => import('../views/cate/OrderPayment'),
  },
  {
    path: '/category/inquiry',
    name: 'CateInquiry',
    component: () => import('../views/cate/Inquiry'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/category/orderPay',
    name: 'OrderPay',
    component: () => import('@/views/cate/orderPay'),
    meta: {
      name: '支付页面',
      requireAuth: true
    }
  },
  {
    path: '/category/paySuccess',
    component: () => import('@/views/cate/successPay'),
    meta: {
      name: '支付成功',
      requireAuth: true
    }
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import('../views/cart/Cart'),
    meta: {
      keepAlive: false, // 不需要被缓存
      requireAuth: true
    }
  },
  {
    path: '/help',
    name: 'Help',
    component: () => import('../views/help/Help'),
    redirect: '/help/center',
    children: [{
        path: '/help/center',
        name: 'HelpCenter',
        component: () => import('../views/help/Center')
      },
      {
        path: '/help/list',
        name: 'HelpCenterList',
        component: () => import('../views/help/List')
      },
      {
        path: '/help/helpDetail',
        name: 'HelpDetail',
        component: () => import('../views/help/detail')
      }
    ]
  },
  {
    path: '/topic',
    name: 'Topic',
    component: () => import('../views/topic/Topic')
  },
  // 新增产业服务页面
  {
    path: '/statyService',
    name: 'Service',
    component: () => import('../views/Service/Service')
  },
  {
    path: '/industryService/service',
    name: 'IndustryService',
    component: () => import('../views/Service/Category')
  },
  {
    path: '/industryService/service/classify/:id',
    name: 'ServiceClassify',
    component: () => import('../views/Service/ServiceClassify')
  },
  {
    path: '/industryService/service/product/:id',
    name: 'ServiceProduct',
    component: () => import('../views/Service/IndustryService')
  },
  {
    path: '/search-company/:keyword',
    name: 'SearchCompany',
    component: () => import('../views/search-company/SearchCompany')
  },
  {
    path: '/search-package/:keyword',
    name: 'SearchPackage',
    component: () => import('../views/search-package/SearchPackage'),
    meta: {
      keepAlive: false
    }
  },
  // 新增发布需求页面
  {
    path: '/releaseRequire',
    name: 'ReleaseRequirement',
    component: () => import('../views/require/ReleaseRequirement'),
    meta: {
      keepAlive: false
    }
  },
  {
    path: '/require',
    name: 'Requirement',
    component: () => import('../views/require/Requirement'),
    redirect: '/require/1',
    children: [{
        path: '/require/1',
        name: 'Requirement1',
        component: () => import('../views/require/children/StepOne'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/require/2',
        name: 'Requirement2',
        component: () => import('../views/require/children/StepTwo'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/require/3',
        name: 'Requirement3',
        component: () => import('../views/require/children/StepThree'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/require/4-1',
        name: 'Requirement4Draft',
        component: () => import('../views/require/children/StepFourDraft'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/require/4-2',
        name: 'Requirement4Submit',
        component: () => import('../views/require/children/StepFourSubmit'),
        meta: {
          requireAuth: true
        }
      }
    ],
    meta: {
      keepAlive: false, // 不需要被缓存
      requireAuth: true
    }
  },
  // 新增委托平台页面
  {
    path: '/commissionOfficial',
    name: 'CommissionOfficial',
    component: () => import('../views/commission-official/CommissionOfficial'),
    meta: {
      title: '委托平台'
    }
  },
  {
    path: '/official',
    name: 'Official',
    component: () => import('../views/official/Official'),
    redirect: '/official/1',
    children: [{
        path: '/official/1',
        name: 'Official1',
        component: () => import('../views/official/children/StepOne'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/official/2',
        name: 'Official2',
        component: () => import('../views/official/children/StepTwo'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/official/3-1',
        name: 'Official3Draft',
        component: () => import('../views/official/children/StepThreeDraft'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/official/3-2',
        name: 'Official3Submit',
        component: () => import('../views/official/children/StepThreeSubmit'),
        meta: {
          requireAuth: true
        }
      }
    ],
    meta: {
      keepAlive: false, // 不需要被缓存
      requireAuth: true
    }
  },
  {
    path: '/demand',
    name: 'Demand',
    component: () => import('../views/demand/Demand')
  },
  {
    path: '/settled',
    name: 'Settled',
    component: () => import('../views/settled/Settled'),
  },
  {
    path: '/advisory',
    name: 'Advisory',
    component: () => import('../views/advisory/Advisory'),
    children: [{
      path: '/advisory/details/:id',
      name: 'Details',
      component: () => import('../views/advisory/children/Details')
    }]
  },
  {
    path: '/exhibition',
    name: 'Exhibition',
    component: () => import('../views/exhibition/Exhibition'),
    children: [{
        path: '/exhibition/exhibition-details/:id',
        name: 'ExhibitionDetails',
        component: () => import('../views/exhibition/children/ExhibitionDetails')
      },
      {
        path: '/exhibition/meeting-details',
        name: 'MeetingDetails',
        component: () => import('../views/exhibition/children/MeetingDetails')
      }
    ]
  },
  // 企业主页
  {
    path: '/company-home/:id',
    name: 'CompanyHome',
    component: () => import('../views/company-home/CompanyHome'),
    meta: {
      companyHome: Symbol()
    },
    children: [{
        path: '/company-home/home/:id',
        name: 'CompanyHomeHome',
        component: () => import('../views/company-home/children/home/Home'),
        meta: {
          companyHome: Symbol()
        }
      },
      {
        path: '/company-home/noSettled/:id',
        component: () => import('@/views/company-home/noSettled'),
        meta: {
          companyHome: Symbol()
        }
      },
      {
        path: '/company-home/product/:id',
        name: 'CompanyHomeProduct',
        component: () => import('../views/company-home/children/product/Product'),
        meta: {
          companyHome: Symbol()
        }
      },
      {
        path: '/company-home/intro/:id',
        name: 'CompanyHomeIntro',
        component: () => import('../views/company-home/children/intro/Intro'),
        meta: {
          companyHome: Symbol()
        }
      },
      {
        path: '/company-home/certifi/:id',
        name: 'CompanyHomeCertifi',
        component: () => import('../views/company-home/children/certifi/Certifi'),
        meta: {
          companyHome: Symbol()
        }
      },
      {
        path: '/company-home/news/:id',
        name: 'CompanyHomeNews',
        component: () => import('../views/company-home/children/news/news'),
        meta: {
          companyHome: Symbol()
        },
        children: [{
          path: '/company-home/news/details/:newsId/:id',
          name: 'CompanyHomeNewsDetails',
          component: () => import('../views/company-home/children/news/details'),
          meta: {
            companyHome: Symbol()
          }
        }]
      },
      {
        path: '/company-home/connect/:id',
        name: 'CompanyHomeConnect',
        component: () => import('../views/company-home/children/connect/Connect'),
        meta: {
          companyHome: Symbol()
        }
      },
      {
        path: '/company-home/problem/:id',
        name: 'CompanyHomeProblem',
        component: () => import('../views/company-home/children/problem/Problem'),
        meta: {
          companyHome: Symbol()
        }
      }
    ]
  },
  // 个人中心
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../views/profile/Profile'),
    redirect: '/profile/welcome',
    meta: {
      requireAuth: true
    },
    children: [
      // 欢迎页
      {
        path: '/profile/welcome',
        name: 'Welcome',
        component: () => import('../views/profile/welcome/Welcome'),
        meta: {
          requireAuth: true
        }
      },

      // 账户设置
      {
        path: '/profile/account-setting/personal-settings',
        name: 'PersonalSettings',
        component: () => import('../views/profile/account-settings/PersonalSettings'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/profile/account-setting/vip-benefits',
        name: 'VipBenefits',
        component: () => import('../views/profile/account-settings/VipBenefits'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/profile/account-setting/account-security',
        name: 'AccountSecurity',
        component: () => import('../views/profile/account-settings/AccountSecurity'),
        meta: {
          requireAuth: true
        }
      },

      // 采购管理
      {
        path: '/profile/purchase-management/my-order',
        name: 'MyOrder',
        component: () => import('../views/profile/purchase-management/MyOrder'),
        meta: {
          requireAuth: true
        },
        children: [{
            path: '/profile/purchase-management/my-order/details',
            name: 'purchaseDetails',
            component: () => import('../views/profile/purchase-management/children/OrderDetails'),
            meta: {
              requireAuth: true
            }
          },
          {
            path: '/profile/purchase-management/my-order/evaluation',
            name: 'Evaluation',
            component: () => import('../views/profile/purchase-management/children/OrderEvaluation'),
            meta: {
              requireAuth: true
            }
          },
          {
            path: '/profile/purchase-management/my-order/express',
            name: 'Express',
            component: () => import('../views/profile/purchase-management/children/OrderExpress'),
            meta: {
              requireAuth: true
            }
          }
        ]
      },
      {
        path: '/profile/purchase-management/my-order/service',
        name: 'MyOrderService',
        component: () => import('../views/profile/purchase-management/MyOrderService'),
        meta: {
          requireAuth: true
        },
        children: [{
            path: '/profile/purchase-management/my-order/service/details',
            name: 'ServiceDetails',
            component: () => import('../views/profile/purchase-management/children/OrderServiceDetails'),
            meta: {
              requireAuth: true
            }
          },
          {
            path: '/profile/purchase-management/my-order/service/evaluation',
            name: 'ServiceEvaluation',
            component: () => import('../views/profile/purchase-management/children/OrderServiceEvaluation'),
            meta: {
              requireAuth: true
            }
          },
          {
            path: '/profile/purchase-management/my-order/service/express',
            name: 'ServiceExpress',
            component: () => import('../views/profile/purchase-management/children/OrderServiceExpress'),
            meta: {
              requireAuth: true
            }
          }
        ]
      },
      {
        path: '/profile/purchase-management/my-inquiry',
        name: 'MyInquiry',
        component: () => import('../views/profile/purchase-management/MyInquiry'),
        meta: {
          requireAuth: true
        },
        children: [{
          path: '/profile/purchase-management/my-inquiry/details',
          name: 'MyInquiryDetails',
          component: () => import('../views/profile/purchase-management/children/InquiryDetails'),
          meta: {
            requireAuth: true
          }
        }]
      },
      {
        path: '/profile/purchase-management/my-demand',
        name: 'MyDemand',
        component: () => import('../views/profile/purchase-management/MyDemand'),
        meta: {
          requireAuth: true
        },
        children: [
          {
          path: '/profile/purchase-management/my-demand/details/:id',
          name: 'MyDemandDetails',
          component: () => import('../views/profile/purchase-management/children/DemandDetails'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/profile/purchase-management/my-demand/merInfo/:id',
          name: 'MyDemandMerInfo',
          component: () => import('../views/profile/purchase-management/children/DemandMerInfo'),
          meta: {
            requireAuth: true
          }
        }
      ]
      },
      {
        path: '/profile/purchase-management/my-consign',
        name: 'MyConsign',
        component: () => import('../views/profile/purchase-management/MyConsign'),
        meta: {
          requireAuth: true
        },
        children: [{
          path: '/profile/purchase-management/my-consign/details/:id',
          name: 'MyConsignDetails',
          component: () => import('../views/profile/purchase-management/children/ConsignDetails'),
          meta: {
            requireAuth: true
          }
        }]
      },

      // 支出管理
      {
        path: '/profile/expenditure-management/expenditure-classification',
        name: 'ExpenditureClassification',
        component: () => import('../views/profile/expenditure-management/ExpenditureClassification'),
        meta: {
          requireAuth: true
        },
        children: [{
          path: '/profile/expenditure-management/expenditure-classification/details',
          name: 'exDetails',
          component: () => import('../views/profile/expenditure-management/children/ExpenditureDetails'),
          meta: {
            requireAuth: true
          }
        }]
      },
      {
        path: '/profile/expenditure-management/expenditure-summary',
        name: 'ExpenditureSummary',
        component: () => import('../views/profile/expenditure-management/ExpenditureSummary'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/profile/expenditure-management/expenditure-total',
        name: 'ExpenditureTotal',
        component: () => import('../views/profile/expenditure-management/ExpenditureTotal'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/profile/expenditure-management/my-supplier',
        name: 'MySupplier',
        component: () => import('../views/profile/expenditure-management/MySupplier'),
        meta: {
          requireAuth: true
        },
        children: [{
          path: '/profile/expenditure-management/my-supplier/details',
          name: 'SupplierDetails',
          component: () => import('../views/profile/expenditure-management/children/SupplierDetails'),
          meta: {
            requireAuth: true
          }
        }]
      },

      // 消息管理
      {
        path: '/profile/message-management/my-message',
        name: 'MyMessage',
        component: () => import('../views/profile/message-management/MyMessage'),
        meta: {
          requireAuth: true
        },
        children: [{
          path: '/profile/message-management/my-message/details',
          name: 'msgDetails',
          component: () => import('../views/profile/message-management/children/Details'),
          meta: {
            requireAuth: true
          }
        }]
      },
      {
        path: '/profile/message-management/notification-management',
        name: 'NotificationManagement',
        component: () => import('../views/profile/message-management/NotificationManagement'),
        meta: {
          requireAuth: true
        }
      },

      // 收货地址
      {
        path: '/profile/receipt-address/receipt-address',
        name: 'ReceiptAddress',
        component: () => import('../views/profile/receipt-address/ReceiptAddress'),
        meta: {
          requireAuth: true
        },
        children: [{
          path: '/profile/receipt-address/add-address',
          name: 'AddAddress',
          component: () => import('../views/profile/receipt-address/children/AddAddress'),
          meta: {
            requireAuth: true
          }
        }]
      },

      // 增票资质
      {
        path: '/profile/invoice-management/invoice-management',
        name: 'InvoiceManagement',
        component: () => import('../views/profile/invoice-management/InvoiceManagement'),
        meta: {
          requireAuth: true,
          keepAlive: false // 不需要被缓存
        },
        children: [{
          path: '/profile/invoice-management/add-invoice',
          name: 'AddInvoice',
          component: () => import('../views/profile/invoice-management/children/AddInvoice'),
          meta: {
            requireAuth: true
          }
        }],
      },

      // 收藏与历史
      {
        path: '/profile/collection-history/collection-products',
        name: 'CollectionProducts',
        component: () => import('../views/profile/collection-history/CollectionProducts'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/profile/collection-history/collection-company',
        name: 'CollectionCompanys',
        component: () => import('../views/profile/collection-history/CollectionCompany'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/profile/collection-history/browsing-history',
        name: 'browsingHistory',
        component: () => import('../views/profile/collection-history/BrowsingHistory'),
        meta: {
          requireAuth: true
        }
      }
    ]
  },
  // 商家入驻
  {
    path: '/merchant-settled',
    name: 'MerchantSettled',
    component: () => import('../views/merchant-settled/MerchantSettled'),
    meta: {
      requireAuth: true,
      keepAlive: false
    },
  }
]

const router = new VueRouter({
  routes,
  // mode: 'history', // TODO: 上线到web服务器中考虑要不要改成history模式
  // base: '/'
})

router.beforeEach((to, from, next) => {
  window.document.title = to.meta.title === undefined ? '找包装网' : '找包装网_' + to.meta.title
  const token = localStorage.getItem('access_token') || ''
  // 未登录，跳转页面不需要登录
  if (!token && !to.meta.requireAuth) {
    next()
    // 未登录，跳转页面需要登录
  } else if (!token && to.meta.requireAuth) {
    // this.$toastVant.fail('登录后才可以操作')
    next({
      name: 'Login'
    })
    // 已登录
  } else {
    next()
  }
})

router.afterEach(() => {
  window.scrollTo(0, 0)
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
